<?php


namespace api\modules\v1\controllers\system\base;

use Yii;
use common\models\base\Project;
use common\helpers\CommonHelper;
use api\controllers\OnAuthController;

class ProjectController extends OnAuthController
{
    public $modelClass = Project::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index-project', 'update-project', 'create-project', 'view-project', 'delete-project','change-status-project']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $optional = [];

    /**
     * v1/system/base/project/index-project
     * $page
     * $pageSize
     *
     * @return mixed|\yii\db\ActiveRecord
     */
    public function actionIndexProject()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 15;

        return Yii::$app->services->project->proList($page,$pageSize);
    }

    /**
     * v1/system/base/project/index-project
     * $page
     * $pageSize
     *
     * @return mixed|\yii\db\ActiveRecord
     */
    public function actionProjectList()
    {
        $params['status'] = Project::STATUS_ENABLE;
        $data['list'] = Yii::$app->services->project->getList($params);
        return $data;
    }

    /**
     * v1/system/base/project/create-project
     * $name 项目名称
     * $name_sx 项目简称
     * $status  1 = 正常  0 = 失效
     *
     * @return mixed|\yii\db\ActiveRecord
     */
    public function actionCreateProject()
    {
        $params = Yii::$app->request->post();
        $data = Yii::$app->services->project->proCreate($params);

        return  CommonHelper::api($data['code'],$data['message']);
    }

    /**
     * v1/system/base/project/delete-project
     * $id
     *
     * @return mixed|\yii\db\ActiveRecord
     */
    public function actionDeleteProject()
    {
        $id = Yii::$app->request->post('id');

        return Yii::$app->services->project->deleteProject($id);
    }

    /**
     * v1/system/base/project/change-status-project
     * $id
     * $status  1 = 正常  0 = 失效
     *
     * @return mixed|\yii\db\ActiveRecord
     * @throws \Exception
     */
    public function actionChangeStatusProject()
    {
        $params = Yii::$app->request->post();
        $status = $params['status'];
        $id = $params['id'];

        return Yii::$app->services->project->changeStatus($id,$status);
    }

    /**
     * v1/system/base/project/update-project
     * $id
     * $status  1 = 正常  0 = 失效
     * $name 项目名称
     * $name_sx 项目缩写
     *
     * @return mixed|\yii\db\ActiveRecord
     * @throws \Exception
     */
    public function actionUpdateProject()
    {
        $params = Yii::$app->request->post();
        $data = Yii::$app->services->project->proUpdate($params,$params['id']);

        return  CommonHelper::api($data['code'],$data['message']);
    }

}